/**
 * Project Name: jxcommons
 * Package Name: org.knoppers.jxcommons
 * created on 21.11.2012 by jkonopka
 */
package org.jenkon.assertion;

/**
 * class with many assertion methods for 'design by contract'.<br>
 * 
 * @author Jens Konopka
 * 
 */
public final class Assert {

	/**
	 * hide public constructor for this util class.<br>
	 */
	private Assert() {
	}

	/**
	 * check the expression of true or false.<br>
	 * if false then <code>throw new {@link IllegalArgumentException}</code>.<br>
	 * 
	 * @see #isTrue(boolean, String)
	 * @param expression expression to check of true or false
	 * @throws IllegalArgumentException throw if expression is false
	 */
	public static void isTrue(final boolean expression) {
		isTrue(expression, "the value must be true");
	}

	/**
	 * check the expression of true or false.<br>
	 * if false then <code>throw new {@link IllegalArgumentException}</code>.<br>
	 * 
	 * @param expression expression to check of true or false
	 * @param message the message of {@link IllegalArgumentException} if expression is false
	 * @throws IllegalArgumentException throw if expression is false
	 */
	public static void isTrue(final boolean expression, final String message) {
		if (!expression) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * check the expression of true or false.<br>
	 * if the expression is true then <code>throw new {@link IllegalArgumentException}</code>.<br>
	 * 
	 * @see #isFalse(boolean, String)
	 * @param expression expression to check of true or false
	 * @throws IllegalArgumentException throw if expression is true
	 */
	public static void isFalse(final boolean expression) {
		isFalse(expression, "the value must be false");
	}

	/**
	 * check the expression of true or false.<br>
	 * if the expression is true then <code>throw new {@link IllegalArgumentException}</code>.<br>
	 * 
	 * @param expression expression to check of true or false
	 * @param message the message of {@link IllegalArgumentException} if expression is true
	 * @throws IllegalArgumentException throw if expression is true
	 */
	public static void isFalse(final boolean expression, final String message) {
		isTrue(!expression, "the value must be false");
	}

	/**
	 * check the obj of is a instance of clazz.<br>
	 * if the obj is not instance of clazz then <code>throw new {@link IllegalArgumentException}</code> with the message 'obj is not a instance of
	 * clazz'.<br>
	 * 
	 * @see #isInstanceOf(Class, Object, String)
	 * @param clazz the required class
	 * @param obj the object to check of is instance of clazz
	 * @throws IllegalArgumentException if the obj is not a instance of clazz
	 */
	public static void isInstanceOf(final Class<?> clazz, final Object obj) {
		isInstanceOf(clazz, obj, String.format("%s is not a instance of %s", obj, clazz));
	}

	/**
	 * check the obj of is a instance of clazz.<br>
	 * if the obj is not instance of clazz then <code>throw new {@link IllegalArgumentException}</code>.<br>
	 * the {@link IllegalArgumentException} contains the message.<br>
	 * 
	 * @param clazz the required class
	 * @param obj the object to check of is instance of clazz
	 * @param message the message of {@link IllegalArgumentException} if obj is not a instance of clazz
	 * @throws IllegalArgumentException if the obj is not a instance of clazz
	 */
	public static void isInstanceOf(final Class<?> clazz, final Object obj, final String message) {
		if (!clazz.isInstance(obj)) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * check if the left is equals the right.<br>
	 * if the left is not equals the right then <code>throw new {@link IllegalArgumentException}</code> with the message 'the left is not equals the
	 * right'.<br>
	 * if the left is <code>null</code> and the right is not <code>null</code>,<br>
	 * then <code>throw new {@link IllegalArgumentException}</code>.<br>
	 * if the left and the right is <code>null</code> then this is okay.<br>
	 * 
	 * @see #isEquals(Object, Object, String)
	 * @param left left to check
	 * @param right right to check
	 * @throws IllegalArgumentException if the left is not equals the right
	 */
	public static void isEquals(final Object left, final Object right) {
		isEquals(left, right, String.format("the %s is not equals the %s.", left, right));
	}

	/**
	 * check if the left is equals the right.<br>
	 * if the left is not equals the right then <code>throw new {@link IllegalArgumentException}</code>.<br>
	 * the {@link IllegalArgumentException} contains the message.<br>
	 * if the left is <code>null</code> and the right is not <code>null</code>,<br>
	 * then <code>throw new {@link IllegalArgumentException}</code>.<br>
	 * if the left and the right is <code>null</code> then this is okay.<br>
	 * 
	 * @param left left to check
	 * @param right right to check
	 * @param message the message for {@link IllegalArgumentException}
	 * @throws IllegalArgumentException if the left is not equals the right
	 */
	public static void isEquals(final Object left, final Object right, final String message) {
		if (left == null && right == null) {
			return;
		}
		if (left == null && right != null) {
			throw new IllegalArgumentException(message);
		}
		if (!left.equals(right)) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * check if the value of is <code>null</code>.<br>
	 * if the value is not <code>null</code>,<br>
	 * then <code>throw new {@link IllegalArgumentException}</code> with the message 'the value is not null'.<br>
	 * 
	 * @see #isNull(Object, String)
	 * @param value the value to check of is <code>null</code>
	 * @throws IllegalArgumentException if the value is not <code>null</code>
	 */
	public static void isNull(final Object value) {
		isNull(value, "the value is not null");
	}

	/**
	 * check if the value of is <code>null</code>.<br>
	 * if the value is not <code>null</code>,<br>
	 * then <code>throw new {@link IllegalArgumentException}</code> with the message.<br>
	 * 
	 * @param value the value to check of is <code>null</code>
	 * @param message the message for the {@link IllegalArgumentException} if the value is not <code>null</code>
	 * @throws IllegalArgumentException if the value is not <code>null</code>
	 */
	public static void isNull(final Object value, final String message) {
		if (value != null) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Check the value of is not <code>null</code>. If the value is <code>null</code> then <code>throw new {@link IllegalArgumentException}</code>
	 * with the message 'the value is null'.
	 * 
	 * @see #isNotNull(Object, String)
	 * @param value value to check is not <code>null</code>
	 * @throws IllegalArgumentException if the value is <code>null</code>
	 */
	public static void isNotNull(final Object value) {
		isNotNull(value, "the value is null");
	}

	/**
	 * check the value of is not <code>null</code>. if the value is <code>null</code> then <code>throw new {@link IllegalArgumentException}</code>
	 * with the message.
	 * 
	 * @param value value to check is not <code>null</code>
	 * @param message the message of the {@link IllegalArgumentException}
	 * @throws IllegalArgumentException if the value is <code>null</code>
	 */
	public static void isNotNull(final Object value, final String message) {
		if (value == null) {
			throw new IllegalArgumentException(message);
		}
	}

}
